package com.zja.servlet;


import com.zja.model.JsonResponse;
import com.zja.service.Impl.LikesServiceImpl;
import com.zja.service.LikesService;
import com.zja.util.ServletUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
/**
 * @author JA
 * @date: 2025/5/5
 * description:获取点赞数
 */
@WebServlet("/api/article/like/count")
public class GetLikeCountServlet extends HttpServlet {
    private final LikesService likesService = new LikesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Long articleId = Long.parseLong(req.getParameter("articleId"));
            int likeCount = likesService.getLikeCount(articleId);
            ServletUtil.writeJsonResponse(resp, JsonResponse.success("获取点赞数成功", likeCount));
        } catch (Exception e) {
            e.printStackTrace();
            ServletUtil.writeJsonResponse(resp, JsonResponse.error("获取点赞数失败: " + e.getMessage()));
        }
    }
}
